/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package client;

import entreprise.Drug;
import entreprise.DrugService;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author duonghau
 */
public class DrugBean {
    private String drugID="";
    private String drugName="";
    private float drugPrice;
    private String drugDescription="";    
    private String message="";
    private String messageStyle="";
    private String drugSearchName="";
    private int page=1;
    private int numberOfRowPerPage=10;
    public DrugBean(){
            
    }
    
    public String getDrugID() {
        return drugID;
    }
    
    public void setDrugID(String drugID) {
        this.drugID = drugID;
    }

    public String getDrugName() {
        return drugName;
    }

    public void setDrugName(String drugName) {
        this.drugName = drugName;
    }

    public float getDrugPrice() {
        return drugPrice;
    }

    public void setDrugPrice(float drugPrice) {
        this.drugPrice = drugPrice;
    }

    public String getDrugDescription() {
        return drugDescription;
    }

    public void setDrugDescription(String drugDescription) {
        this.drugDescription = drugDescription;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getMessageStyle() {
        return messageStyle;
    }

    public void setMessageStyle(String messageStyle) {
        this.messageStyle = messageStyle;
    }

    public String getDrugSearchName() {
        return drugSearchName;
    }

    public void setDrugSearchName(String drugSearchName) {
        this.drugSearchName = drugSearchName;
    }
    
    public ArrayList<Drug> getDrugs(){
        DrugService drugService=new DrugService();
        ArrayList<Drug> drugs=drugService.drugs(drugSearchName,(page-1)*numberOfRowPerPage,numberOfRowPerPage);
        return drugs;        
    }
    
    public ArrayList<Integer> getPagings(){
        DrugService drugService=new DrugService();        
        int numberOfRow=drugService.getNumberOfDrugs(drugSearchName);
        int numberOfPage=(numberOfRow/numberOfRowPerPage);
        if(numberOfRow%numberOfRowPerPage!=0){
            numberOfPage+=1;
        }
        ArrayList<Integer> paging=new ArrayList<>();
        for(int i=1;i<=numberOfPage;i++){
            paging.add(i);
        }
        return paging;
    }
    
    public void getDrugDetail(){
        if(!drugID.equals("")){
            DrugService drugService=new DrugService();
            Drug drug = drugService.getDrug(drugID);
            if(drug!=null){
                drugName=drug.getDrugName();
                drugDescription=drug.getDrugDescription();
                drugPrice=drug.getDrugPrice();
            }
        }        
    }
    public String addDrug(){
        DrugService drugService=new DrugService();
        if(drugService.createDrug("", drugName, drugPrice, drugDescription)){
            message="Drug add success";
            messageStyle="success";            
        }else{
            message="Drug add error";
            messageStyle="error";
        }  
        return "drugs";
    }
    public String editDrug(){
        DrugService drugService=new DrugService();
        if(drugService.updateDrug(drugID, drugName, drugPrice, drugDescription)){
            message="Drug update success";
            messageStyle="success";            
        }else{
            message="Drug update error";
            messageStyle="error";            
        }
        return "drugs";
    }
    public String deleteDrug(String drugId){
        DrugService drugService=new DrugService();
        if(drugService.deleteDrug(drugId)){
            message="Drug delete success";
            messageStyle="success";             
        }else{
            message="Drug delete error";
            messageStyle="error";             
        }
        return "drugs";
    }
}
